<?php
//后台登录控制器
class LoginController{
	//显示登录页面
	public function loginAction(){
		
		include VIEWS_PATH . "login.html";
	}

	//验证登录操作
	public function signinAction(){
		//1.收集表单数据，用户名和密码
		$username = trim($_POST['username']);
		$password = trim($_POST['password']);
		$code=$_POST['code'];
		//echo $_SESSION['captcha'];
		//var_dump($_POST);
		if ($code!=$_SESSION['captcha']){
			echo "<script>alert('验证码错误');window.location.href='index.php?c=login&a=login'</script>";
			die();
			//$this->jump('index.php?c=login&a=login','验证码不不正确!');
		}
		//单引号转义
		$username = addslashes($username);
		$password = addslashes($password);
		
		//2.验证和处理
		if ($username == '') {
			echo "<script>alert('用户名为空!');window.location.href='index.php?c=login&a=login'</script>";
		}
		if ($password == '') {
			echo "<script>alert('密码为空');window.location.href='index.php?c=login&a=login'</script>";
		}
		//3.调用模型完成验证操作，给出相应提示，并跳转
		$adminModel = new AdminModel('tb_manager');
		$user = $adminModel->checkUser($username,$password);
		
		if ($user==1) {
			//ok,登录成功，保存登录标识，跳转
			$_SESSION['admin_name'] = $username;
			$_SESSION['admin_pwd']=$password;
			header("location:index.php?c=index&a=index");
		} else {
			//failer
			echo "<script>alert('用户名或者密码错误!');window.location.href='index.php?c=login&a=login'</script>";
		}
	}

	//退出
	public function logoutAction(){
		//销毁session
		unset($_SESSION['admin_name']); //可加可不加 但不能这么写，unset($_SESSION);
		session_destroy();
		//跳转
		header("location:index.php?c=login&a=login");
	}

	//生成验证码
	public function captchaAction(){
		//载入验证码类
		
		include LIB_PATH."Captcha.class.php";
		//实例化对象
		$captcha = new Captcha();
		//生成验证码
		$captcha->generateCode();
		//将其保存到session
		$_SESSION['captcha'] = $captcha->getCode();
	}
}

